package com.jd.risk.bankorder.controller.privilege;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jd.risk.bankorder.common.CommonResult;
import com.jd.risk.bankorder.controller.BaseController;
import com.jd.risk.bankorder.entity.privilege.Resource;
import com.jd.risk.bankorder.entity.privilege.Role;
import com.jd.risk.bankorder.service.privilege.ResourceService;
import com.jd.risk.bankorder.service.privilege.RoleService;

@Controller
@RequestMapping("/privilege/resource")
public class ResourceController extends BaseController{
	
	@Autowired
	private ResourceService resourceService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/toResourceMangerPage")
	public String toResourceMangerPage()
	{
		return "/privilege/resourceMangerPage";
	}
	
	@ResponseBody
	@RequestMapping("/queryResourceNode")
	public String queryResourceNode(@RequestParam(value="roleId" , required=false) Long roleId)
	{
		List<Resource> res = resourceService.getReourceNodes();
		if(null!=roleId)
		{
			Role role = roleService.getRoleByRoleId(roleId);
			if(null!=role && StringUtils.isNotBlank(role.getResourceIds()) && role.getResourceIds().contains(","))
			{
				String resourcestr = role.getResourceIds();
				List<String> resourceIds = java.util.Arrays.asList( resourcestr.split(","));
				
				for(Resource resource :res)
				{
					if(resourceIds.contains(resource.getId().toString()))
					{
						resource.setChecked(true);
					}
				}
			}
		}
		return toJson(res);
	}
	@ResponseBody
	@RequestMapping(value="/saveOrUpdateResource")
	public String saveOrUpdateResource( Resource resource)
	{
	    CommonResult<Resource> result = 	resourceService.saveOrUpdateResource(resource);
		return toJson(result);
	}
	
	@ResponseBody
	@RequestMapping("/deleteResource")
	public String deleteResource(Resource resource){
			
	    CommonResult<String> result = 	resourceService.deleteResource(resource);
		return toJson(result);
	}

	@ResponseBody
	@RequestMapping("/queryAllMenus")
	public String getAllMenus()
	{
		List<Resource> resources = resourceService.getAllMenus();
		return toJson(resources);
	}

}
